/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SocketBusiness;

import DTO.ControlDTO;
import MyLogging.ILog;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Administrator
 */
public class ServerListen implements Runnable {

    private int defaultPort;
    private ServerSocket server;
    private Socket client;
    private OutputStream outStream;
    private InputStream inStream;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private String systemSign;
    private ControlDTO operation;

    public ServerListen(String systemSign, int port, ControlDTO operation) {
        this.defaultPort = port;
        this.systemSign = systemSign;
        this.operation = operation;
    }

    public boolean openServer() {
        try {
            server = new ServerSocket(this.defaultPort);
            int localPort = server.getLocalPort();
            ILog.writeLog("Server is listening on port " + localPort, systemSign);
            client = server.accept();
            String destName = client.getInetAddress().getHostName();
            int destPort = client.getPort();
            ILog.writeLog("Accecpted connection from " + localPort + " to destName : " + destName + " destport : " + destPort, systemSign);

            inStream = new DataInputStream(client.getInputStream());
            ois = new ObjectInputStream(inStream);

//            outStream = new DataOutputStream(client.getOutputStream());
//            oos = new ObjectOutputStream(outStream);

            return true;
        } catch (IOException e) {
            ILog.writeLog("Server Open Failure !!! ", systemSign);
            e.printStackTrace();
        }
        return false;
    }

    public boolean closeServer() {
        try {
            inStream.close();
            outStream.close();
            client.close();
            server.close();
            ILog.writeLog("Connection close", systemSign);
            return true;
        } catch (IOException e) {
            ILog.writeLog("Close connection fail  !!! ", systemSign);
            e.printStackTrace();
        }
        return false;
    }

    public void business() {
        ILog.writeLog("Business Not Installed", systemSign);
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object receiveMessage() throws ClassNotFoundException {
        try {
            Object ob = this.ois.readObject();
            System.out.println(this.getClass() + " DA NHAN DUOC OBJECT : >>>>>>>>>>>>>>>>>> : " + ob.getClass().toString());
            return ob;
        } catch (IOException e) {
            System.out.println("Receive Message ERROR !!! ");
            e.printStackTrace();
        }
        return null;
    }

    public boolean sendMessage(Object ob) {
        try {
            this.oos.writeObject(ob);
            //this.oos.flush();
            return true;
        } catch (IOException e) {
            ILog.writeLog("Send Message ERROR !!! ", systemSign);
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void run() {
        if (!openServer()) {
            return;
        }
        business();
        closeServer();
    }

    public Socket getClient() {
        return client;
    }

    public void setClient(Socket client) {
        this.client = client;
    }

    public int getDefaultPort() {
        return defaultPort;
    }

    public void setDefaultPort(int defaultPort) {
        this.defaultPort = defaultPort;
    }

    public InputStream getInStream() {
        return inStream;
    }

    public void setInStream(InputStream inStream) {
        this.inStream = inStream;
    }

    public ObjectInputStream getOis() {
        return ois;
    }

    public void setOis(ObjectInputStream ois) {
        this.ois = ois;
    }

    public ObjectOutputStream getOos() {
        return oos;
    }

    public void setOos(ObjectOutputStream oos) {
        this.oos = oos;
    }

    public OutputStream getOutStream() {
        return outStream;
    }

    public void setOutStream(OutputStream outStream) {
        this.outStream = outStream;
    }

    public ServerSocket getServer() {
        return server;
    }

    public void setServer(ServerSocket server) {
        this.server = server;
    }

    public String getSystemSign() {
        return systemSign;
    }

    public void setSystemSign(String systemSign) {
        this.systemSign = systemSign;
    }

    public ControlDTO getOperation() {
        return operation;
    }

    public void setOperation(ControlDTO operation) {
        this.operation = operation;
    }
}